package com.hiv.service;

import cn.hutool.core.convert.Convert;
import com.hiv.common.PageInfo;
import com.hiv.common.WebResponse;
import com.hiv.common.param.page.PageQuerySamplingBoxRecordParam;
import com.hiv.common.utils.AssertUtil;
import com.hiv.mapper.SamplingBoxRecordMapper;
import com.hiv.mapper.SamplingRecordMapper;
import com.hiv.mapper.domain.Admin;
import com.hiv.mapper.domain.SamplingBoxRecord;
import com.hiv.mapper.domain.SamplingRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 采样数据盒子关系Service业务层处理
 *
 * @author zhanghong
 * @date 2022-09-20
 */
@Service
public class SamplingBoxRecordService {
    @Autowired
    private SamplingBoxRecordMapper samplingBoxRecordMapper;
    @Autowired
    private SamplingRecordMapper samplingRecordMapper;

    /**
     * 分页查询
     * @param param
     * @return
     */
    public PageInfo<SamplingBoxRecord> pageQuery(PageQuerySamplingBoxRecordParam param) {
        long count = samplingBoxRecordMapper.pageCount(param);
        List<SamplingBoxRecord> list = samplingBoxRecordMapper.pageQuery(param, param.getPageSize(), param.fetchOffSet());
        return new PageInfo<>(param.getPageNum(), param.getPageSize(), list, count);
    }

    /**
     * 查询采样数据盒子关系
     *
     * @param id 采样数据盒子关系主键
     * @return 采样数据盒子关系
     */
    public SamplingBoxRecord getById(Long id) {
        return samplingBoxRecordMapper.selectById(id);
    }

    /**
     * 查询采样数据盒子关系列表
     *
     * @param samplingBoxRecord 采样数据盒子关系
     * @return 采样数据盒子关系
     */
    public List<SamplingBoxRecord> selectList(SamplingBoxRecord samplingBoxRecord) {
        List<SamplingBoxRecord> list = samplingBoxRecordMapper.selectList(samplingBoxRecord);
        list.stream().forEach(item->buildInfo(item));
        return list;
    }

    private void buildInfo(SamplingBoxRecord item) {
        SamplingRecord samplingRecord = samplingRecordMapper.selectById(item.getSamplingId());
        if (samplingRecord != null) {
            item.setBarCode(samplingRecord.getBarCode());
            item.setCheckType(samplingRecord.getCheckType());
        }
    }

    /**
     * 保存采样数据盒子关系
     *
     * @param samplingBoxRecord 采样数据盒子关系
     * @return 结果
     */
    public Boolean save(SamplingBoxRecord samplingBoxRecord) {
        if (samplingBoxRecord.getId() != null) {
            samplingBoxRecordMapper.update(samplingBoxRecord);
            return true;
        }
        samplingBoxRecordMapper.insert(samplingBoxRecord);
        return true;
    }

    /**
     * 批量删除采样数据盒子关系
     *
     * @param ids 需要删除的采样数据盒子关系主键
     * @return 结果
     */
    public Boolean deleteByIds(String ids) {
        samplingBoxRecordMapper.deleteByIds(Convert.toStrArray(ids));
        return true;
    }

    /**
     * 绑定
     * @param param
     * @return
     */
    public WebResponse<Boolean> bind(Admin admin, SamplingBoxRecord param) {
        SamplingRecord samplingRecord = samplingRecordMapper.queryByBarCode(param.getBarCode());
        AssertUtil.assertNotNull(samplingRecord, "数据不存在");
        if (samplingRecord.getSamplingStatus() != 1) {
            //标识
            return WebResponse.build(false, "添加失败");
        }

        // 校验当前账号与条码是否是同一家医院
        AssertUtil.assertEqual(samplingRecord.getHospitalId().intValue(), admin.getHospitalId().intValue(), "条码和当前医院不匹配");

        SamplingBoxRecord existBind = samplingBoxRecordMapper.checkIsBind(samplingRecord.getId());
        AssertUtil.assertNull(existBind, "条码已绑定");

        param.setSamplingId(samplingRecord.getId());
        samplingBoxRecordMapper.insert(param);
        return WebResponse.build(true);
    }
}
